<template>
  <div class="container">
    <div class="main">
      <div class="pay-title">
        支付总金额 <span class="pay-price">￥ {{ payData.price }}</span>
      </div>
      <div class="pay-main">
        <h4>微信支付</h4>
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          class="pay-qrcode"
        >
          <div class="qrcode">
            <!-- 二维码 -->
            <canvas id="qrcode-stage" ref="canvas"></canvas>
            <p>请使用微信扫一扫</p>
            <p>扫描二维码支付</p>
          </div>
          <div class="pay-example">
            <img src="http://157.122.54.189:9093/images/wx-sweep2.jpg" alt="" />
          </div>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import QRCode from "qrcode";
export default {
  data() {
    return {
      payData: {},
      timer: null
    };
  },
  watch: {
    "$store.state.user.userInfo.token": {
      handler() {
        if (this.$store.state.user.userInfo.token) {
          this.loadPage();
        }
      },
      immediate: true
    }
    // watch 是在数据有变化是触发, 但是页面进来的第一时间, 不会触发
    // 只能靠 mounted
    // 如果我们想连 mounted 的事情都干了, 一进来就触发 watch
    // 可以使用 immediate 属性
  },
  methods: {
    checkPayment() {
      this.$axios({
        url: "/airorders/checkpay",
        method: "post",
        headers: {
          Authorization: "Bearer " + this.$store.state.user.userInfo.token
        },
        data: {
          id: this.payData.id,
          nonce_str: this.payData.price,
          out_trade_no: this.payData.payInfo.order_no
        }
      }).then(res => {
        this.parsePaymentResult(res.data.trade_state);

        console.log(res.data);
        if (res.data.trade_state == "NOTPAY") {
          // 发现支付没有完成, 设置一个定时器, 定时调用自己
          this.timer = setTimeout(() => {
            this.checkPayment();
          }, 2000);
        } else if (res.data.trade_state == "SUCCESS") {
          this.$message.success("支付成功");
        }
      });
    },
    parsePaymentResult(state) {
      switch (state) {
        case "SUCCESS":
          console.log("支付成功");
          break;

        case "REFUND":
          console.log("转入退款");
          break;

        case "NOTPAY":
          console.log("未支付");
          break;

        case "CLOSED":
          console.log("已关闭");
          break;

        case "REVOKED":
          console.log("已撤销");
          break;

        case "USERPAYING":
          console.log("用户支付中");
          break;

        case "PAYERROR":
          console.log("支付失败");
          break;

        default:
          break;
      }
    },
    loadPage() {
      this.$axios({
        url: "/airorders/" + this.$route.query.id,
        headers: {
          Authorization: "Bearer " + this.$store.state.user.userInfo.token
        }
      }).then(res => {
        console.log(res.data);
        this.payData = res.data;
        // 在数据读取完毕的时候, 就有了 支付链接
        // 这时候就应该生成支付二维码了
        // 需要两个元素 1. canvas 2. 字符串
        // canvas 可以用原生 js 获取, 也可以用 vue ref 获取
        const canvas = document.querySelector("#qrcode-stage");
        // const canvas = this.$refs.canvas
        const qrcodeStr = res.data.payInfo.code_url;
        QRCode.toCanvas(canvas, qrcodeStr, {
          width: 200
        });

        this.checkPayment();
      });
    }
  },
  beforeDestroy() {
    // 跳出页面之前应该清理定时器
    // 避免遗漏
    console.log("跳出支付页");
    console.log("清除定时器");
    clearTimeout(this.timer);
  }
};
</script>

<style scoped lang="less">
.container {
  background: #f5f5f5;
  padding: 30px 0;

  .main {
    width: 1000px;
    margin: 0 auto;

    .pay-title {
      text-align: right;
      span {
        font-size: 28px;
        color: orangered;
      }
    }

    .pay-main {
      background: #fff;
      margin-top: 10px;
      border-top: 5px orange solid;
      padding: 30px;

      h4 {
        font-size: 28px;
        font-weight: normal;
        margin-bottom: 10px;
      }

      .pay-qrcode {
        padding: 0 80px;
      }

      .qrcode {
        border: 1px #ddd solid;
        padding: 15px;
        height: fit-content;

        #qrcode-stage {
          width: 200px;
          height: 200px;
          margin-bottom: 10px;
        }

        p {
          line-height: 2;
          text-align: center;
        }
      }
    }
  }
}
</style>
